package com.hyjx.framework.component.interfaceservice.dataservice;

import com.hyjx.framework.component.interfaceservice.InterFaceConstants;
import com.hyjx.framework.component.interfaceservice.UserInfoModel;
import com.hyjx.framework.component.mutidatasource.datasources.DataSourcesModel;
import com.hyjx.framework.orm.BaseService;
import com.hyjx.framework.orm.dao.IBaseDao;
import com.hyjx.framework.orm.dao.Pager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据服务服务实现类
 * @author patrick
 */
@Service
@Transactional
public class DataServiceServiceImpl extends BaseService implements DataServiceService {
	
	@Resource
	private IBaseDao dao;
	
	private static final String MODULE_NAME = InterFaceConstants.INTERFACE_MODULE_NAME;
	
	private static final String DATA_SERVICE_MODULE_NAME = "com.hyjx.framework.component.interfaceservice.dataservice";
	
	@Override
	public boolean addCondition(SrvConditionModel model) {
		return dao.addObject(DATA_SERVICE_MODULE_NAME + ".insertCondition", model);
	}
	@Override
	public String queryCondition(String str) {
		return dao.getPageJsonStr(DATA_SERVICE_MODULE_NAME + ".queryListCondition",DATA_SERVICE_MODULE_NAME + ".queryListConditionCount", str);
	}
	@Override
	public int deleteCondition(SrvConditionModel model) {
		return dao.deleteObject(DATA_SERVICE_MODULE_NAME + ".deleteCondition", model);
	}
	
	@Override
	public SrvConditionModel queryConditionByid(String str) {
		return (SrvConditionModel) dao.queryObject(DATA_SERVICE_MODULE_NAME + ".queryListConditionById",str);
	}
	
	
	@Override
	public int updateCondition(SrvConditionModel model) {
		
		return dao.updateObject(DATA_SERVICE_MODULE_NAME + ".updateCondition", model);
	}
	
	
	//服务预览接口功能
	@Override
	public List serviceReview(String sql) {
			return dao.queryObjectList(DATA_SERVICE_MODULE_NAME + ".reviewService", sql);
		}
	
	//查询参数条件
	@Override
	public List queryDataConditionService(String str){
		return dao.queryObjectList(MODULE_NAME + ".searchDataConditionService",str);
	}
	
	
	//分页查找
	@Override
	public List queryPager(String sql,String pageNo,String pageSize){
		
			int int_no = Integer.parseInt(pageNo);
			int int_size = Integer.parseInt(pageSize);
			
			int ibegin = (int_no - 1) * int_size;


			Pager pager = dao.getPager(MODULE_NAME + ".Servicetemp", MODULE_NAME + ".ServicetempCount", sql, ibegin, int_size);
			return pager.getResultList();
		}
	
	//分页查找1
	@Override
	public Map queryPagerP(String sql,String pageNo,String pageSize){
			List list = null;
			List listNum=null;
			List finlist=new ArrayList<Object>();
			int int_no = Integer.parseInt(pageNo);
			int int_size = Integer.parseInt(pageSize);
			
			int ibegin = (int_no - 1) * int_size;
			//sql="select * from srv_userinfo";
			//SqlMapClientDaoSupport sqlmap=new SqlMapClientDaoSupport();
			//sql = sql.toLowerCase();
			//String sql1="select * from a_2015";
			//listNum=dao.queryObjectList(MODULE_NAME + ".Servicetemp", sql1);

			//这里可能 会造成内存溢出
			
			//over ceshi
			
			listNum=dao.queryObjectList(MODULE_NAME + ".Servicetemp", sql);
			int size1=listNum.size();
			if(ibegin<size1&&ibegin>=0){
				for(int i=ibegin;i<ibegin+int_size;i++){
					if(i<size1){
						Object o=listNum.get(i);
						finlist.add(o);
					}else{
						break;
					}
					
				}
			}
			
			//SqlMapClientTemplate t=dao.getSqlMapClientTemplate1();
			//list=t.queryForList(MODULE_NAME + ".Servicetemp", sql, ibegin, int_size);
			//list =dao.getSqlMapClientTemplate1().queryForList(MODULE_NAME + ".Servicetemp", sql, ibegin, int_size);
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("list", finlist);
			map.put("total", ""+listNum.size());
			return map;
		}
	
	
	//删除服务时判断该服务是否已经被使用
	public int condel(String srvid){
		String temp= (String) dao.queryObject(DATA_SERVICE_MODULE_NAME + ".conditiondel",srvid);
		int count=Integer.parseInt(temp);
		return count;
	}
	
	
	//级联删除服务
	@Override
	public int deleteClear(DataServiceModel model) {
		String srvid=model.getSrvid();
		dao.deleteObject(DATA_SERVICE_MODULE_NAME + ".deleteConditionBySrvId",srvid);
		dao.deleteObject(DATA_SERVICE_MODULE_NAME + ".delete",model);
		
		return 1;
		/* try {  

		//	 SqlMapClient sqlMap = dao.getSqlMapClientTemplate1().getSqlMapClient();; 
			 sqlMap.startTransaction();
             int temp1= sqlMap.delete(DATA_SERVICE_MODULE_NAME + ".delete", model);  
             String srvid=model.getSrvid();
     		 int temp2=sqlMap.delete(DATA_SERVICE_MODULE_NAME + ".deleteConditionBySrvId", srvid);
     		 sqlMap.commitTransaction();
     		  
             
             if(temp1>0&&temp2>0){
     			return 1;
     		}else{
     			return 0;
     		}

    } finally {  

    			sqlMap.endTransaction();   

    } 
*/
	}
	
	
	@Override
	public String sqlConfirm(String str){
		String fin="select count(*) from ("+str+") as t ;";
		
		return (String)dao.queryObject(DATA_SERVICE_MODULE_NAME + ".sqlconfirm", str);
		
	}
	
	
	@Override
	public int namelimit(DataServiceModel model){
		
		String temp= (String) dao.queryObject(DATA_SERVICE_MODULE_NAME + ".namelimit",model);
		int count=Integer.parseInt(temp);

		return count;
		
	}
	
	//得到私钥
	@Override
	public UserInfoModel findUserInfo(String userid){
		return (UserInfoModel) dao.queryObject(MODULE_NAME + ".searchUserInfo",userid);
	}
	
	@Override
	public int editMy(DataServiceModel model) {
		//System.out.println("111");
		//int temp=dao.updateObject(DATA_SERVICE_MODULE_NAME + ".update", model);
		return dao.updateObject(DATA_SERVICE_MODULE_NAME + ".update", model);
	}
	
	
	/**
	 * 查询符合条件的的数据源（包括所有的）
	 * 
	 */
	@Override
	public List serviceAllDs(DataSourcesModel model) {
		return dao.queryObjectList("datasources.queryList", model);
	}
}
